package com.ghd.dataviz.core.constant;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * The class Error code enum.
 *
 * @author guohd
 */
@Getter
@AllArgsConstructor
public enum ErrorCodeEnum {
	/**
	 * Gl 99990100 error code enum.
	 */
	GL99990100(9999100, "参数异常"),
	/**
	 * Gl 99990401 error code enum.
	 */
	GL99990401(99990401, "无访问权限"),
	/**
	 * Gl 000500 error code enum.
	 */
	GL99990500(500, "未知异常"),
	/**
	 * Gl 000403 error code enum.
	 */
	GL99990403(9999403, "无权访问"),
	/**
	 * Gl 000404 error code enum.
	 */
	GL9999404(9999404, "找不到指定资源"),
	/**
	 * Gl 99990001 error code enum.
	 */
	GL99990001(99990001, "注解使用错误"),
	/**
	 * MQ 10050001 error code enum.
	 */
	// 1005 任务中心
	MQ10050001(10050001, "消息的消费Topic不能为空"),
	/**
	 * MQ 10050002 error code enum.
	 */
	MQ10050002(10050002, "根据消息key查找的消息为空"),
	/**
	 * MQ 10050003 error code enum.
	 */
	MQ10050003(10050003, "删除消息失败,messageKey=%s"),
	/**
	 * MQ 10050004 error code enum.
	 */
	MQ10050004(10050004, "消息中心接口异常,message=%s, messageKey=%s"),
	/**
	 * MQ 10050005 error code enum.
	 */
	MQ10050005(10050005, "目标接口参数不能为空"),
	/**
	 * MQ 10050006 error code enum.
	 */
	MQ10050006(10050006, "根据任务Id查找的消息为空"),
	/**
	 * MQ 10050007 error code enum.
	 */
	MQ10050007(10050007, "消息数据不能为空"),
	/**
	 * MQ 10050008 error code enum.
	 */
	MQ10050008(10050008, "消息体不能为空,messageKey=%s"),
	/**
	 * MQ 10050009 error code enum.
	 */
	MQ10050009(10050009, "消息KEY不能为空"),
	/**
	 * MQ 100500010 error code enum.
	 */
	MQ100500010(10050010, "Topic=%s, 无消费者订阅"),
	/**
	 * MQ 100500011 error code enum.
	 */
	MQ100500011(10050011, "Mq编码转换异常, MessageKey=%s"),
	/**
	 * MQ 100500012 error code enum.
	 */
	MQ100500012(10050012, "发送MQ失败, MessageKey=%s"),
	/**
	 * MQ 100500013 error code enum.
	 */
	MQ100500013(10050013, "延迟级别错误, Topic=%s, MessageKey=%s"),
	/**
	 * MQ 100500014 error code enum.
	 */
	MQ100500014(10050014, "MQ重试三次,仍然发送失败, Topic=%s, MessageKey=%s"),
	/**
	 * MQ 100500015 error code enum.
	 */
	MQ100500015(10050015, "消息PID不能为空, messageKey=%s");

	@JsonValue
	private int code;
	private String msg;

	private static Map<Integer, ErrorCodeEnum> map;

	static {
		map = Stream.of(ErrorCodeEnum.values())
				.collect(Collectors.toMap(ErrorCodeEnum::getCode, Function.identity(),
						(k, v) -> k.ordinal() > v.ordinal() ? k : v ));
	}

	@JsonCreator
	public static ErrorCodeEnum of(Integer code) {
		return map.get(code);
	}
}
